import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

//!组合式写法
// export const useStore = defineStore('counter', () => {
//   const count = ref(0)
//   const name = ref('Syaro')
//   const isAdmin = ref(true)
//   const items = ref([])
//   const hasChanged = ref(true)
//   const doubleCount = computed(() => count.value * 2)
//   const doublePlusone = computed(() => doubleCount.value + 1)
//   function increment() {
//     count.value++
//   }
//   function reset() {
//     count.value = 0
//     name.value = 'Syaro'
//     isAdmin.value = true
//     items.value = []
//     hasChanged.value = true
//   }
//   return { reset, count, doubleCount, increment, name, isAdmin, items, hasChanged, doublePlusone }
// })

//!选项式写法
export const useStore = defineStore('main', {
  state: () => {
    return {
      count: 0,
      name: 'Syaro',
      isAdmin: true,
      items: [],
      hasChanged: true
    }
  },
  getters: {
    doubleCount: (state) => state.count * 2,
    doublePlusone(): number {
      return this.doubleCount + 1
    }
  },
  actions: {
    increment() {
      this.count++
    },
    //重置数据
    reset() {
      this.$reset();
    }
  },
  // persist: true
  //自定义数据持久化
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'useStore',
        storage: localStorage,
        paths: ['name'],
        serializer: {
          serialize: (vlue: number) => btoa(JSON.stringify(vlue)),
          deserialize: (vlue: string) => JSON.parse(atob(vlue))
        }
      }
    ],
  },

})



